using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxClasses.Internal;

namespace FeatureCenter.Web.Controls {
    public class StarRatingControl : ASPxWebControl, IRequiresLoadPostDataControl {
        private Table table;
        private int _value = 0;
        private int maxValue = 5;
        private string imageOverUrl;
        private bool autoPostback = false;
        private string imageOffUrl;
        private string imageOnUrl;
        private string HiddenFieldName {
            get {
                return (ClientID + "_Value");
            }
        }
        protected override bool LoadPostData(System.Collections.Specialized.NameValueCollection postCollection) {
            string clientState = postCollection[HiddenFieldName];
            if(string.IsNullOrEmpty(clientState)) {
                return false;
            }
            int newValue = Convert.ToInt32(clientState);
            bool changed = newValue != Value;
            if(changed) {
                Value = newValue;
                OnValueChanged(new EventArgs());
            }
            return changed;
        }
        protected override void RegisterIncludeScripts() {
            base.RegisterIncludeScripts();
            RegisterIncludeScript(typeof(StarRatingControl), "FeatureCenter.Web.Controls.StarRatingControl.js");
        }
        protected override void RegisterHiddenFields() {
            RegisterHiddenField(HiddenFieldName, Value.ToString());
        }
        protected virtual void OnValueChanged(EventArgs e) {
            if (ValueChanged != null) {
                ValueChanged(this, e);
            }
        }
        protected override string GetClientObjectClassName() {
            return "ASPxClientStarRatingControl";
        }
        protected override void CreateControlHierarchy() {
            table = new Table();
            table.ID = "MainTable";
			table.Height = 22;
			table.BorderWidth = 0;
			table.CellPadding = 0;
			table.CellSpacing = 0;
			table.Rows.Add(new TableRow());
            Controls.Add(table);
        }
        protected override void GetCreateClientObjectScript(System.Text.StringBuilder stb, string localVarName, string clientName) {
            base.GetCreateClientObjectScript(stb, localVarName, clientName);
            stb.AppendFormat(@"{0}.SetSettings(""{1}"", ""{2}"", ""{3}"", {4}, {5}, {6});", localVarName, ImageOnUrl, ImageOffUrl, ImageOverUrl, MaxValue, Value, Enabled ? "true" : "false");
        }
        protected override void PrepareControlHierarchy() {
            RenderUtils.SetVisibility(this, IsClientVisible(), true);
            table.Attributes["Value"] = Value.ToString();
            for(int i = 1; i <= MaxValue; i++) {
                Image image = new Image();
                image.ID = String.Format("img{0}", i);
                image.ImageUrl = (i <= Value) ? ImageOnUrl : ImageOffUrl;
                image.Attributes["imageSrc"] = image.ImageUrl;
                image.Attributes["imageHover"] = ImageOverUrl;
                image.Attributes["value"] = i.ToString();
                image.BorderStyle = BorderStyle.None;
                TableCell cell = new TableCell();
                cell.Controls.Add(image);
                table.Rows[0].Cells.Add(cell);
            }
        }
        protected override bool IsScriptEnabled() {
            return true;
        }
        public virtual bool AutoPostback {
            get { return autoPostback; }
            set { autoPostback = value; }
        }
        public string ImageOffUrl {
            get { return imageOffUrl; }
            set { imageOffUrl = value; }
        }
        public string ImageOnUrl {
            get { return imageOnUrl; }
            set { imageOnUrl = value; }
        }
        public string ImageOverUrl {
            get { return imageOverUrl; }
            set { imageOverUrl = value; }
        }
        public int MaxValue {
            get { return maxValue; }
            set { maxValue = value; }
        }
        public int Value {
            get { return _value; }
            set { _value = value; }
        }
        public override void Dispose() {
            if(table != null) {
                foreach(TableCell cell in table.Rows[0].Cells) {
                    foreach(WebControl control in cell.Controls) {
                        control.Dispose();
                    }
                    cell.Controls.Clear();
                }
                table.Rows[0].Cells.Clear();
                table.Dispose();
                table = null;
            }
            base.Dispose();
        }
        public event EventHandler ValueChanged;
        public StarRatingControl() {
            EnableClientSideAPIInternal = true;
        }
    }
}
